//
//  HMEmoticonToolBar.swift
//  Weibo
//
//  Created by 李学林 on 15/12/15.
//  Copyright © 2015年 tete. All rights reserved.
//

import UIKit
// 按钮点击的协议
protocol HMEmoticonToolBarDelegate: NSObjectProtocol{
    func didselectedButtonWithType(type: HMEmoticonType)
}

// 枚举,用来包装tag值,使得代码更具有可读性
enum HMEmoticonType: Int {
    case Recent = 1000
    case Default = 1001
    case Emoji = 1002
    case Lxh = 1003
}
class HMEmoticonToolBar: UIStackView {

    weak var delegate: HMEmoticonToolBarDelegate?
    
    var currentSelectedButton: UIButton?
    
  //*****************************************************************
    var currentSection: Int? {
        didSet{
            
            guard let section = currentSection else {
                return
            }
            
            
            // 判断如果当前选中的 button 的 tag 与 传入的 section(加上基数) 一样,直接返回
            // 不做任何
            if currentSelectedButton?.tag == section + 1000 {
                return
            }
            // 1. 取到 section 对应的按钮
            // 通过一个 tag 取到 tag 对应的子控件
            let button = viewWithTag(section + 1000)! as! UIButton
            //            // 2. 让之前选中的按钮取消选中
            //            currentSelectedButton?.selected = false
            //            // 3. 让第 1 步取到的按钮选中
            //            button.selected = true
            //            // 4. 将当前选中的按钮赋值给 currentSelectedButton
            //            currentSelectedButton = button
            selectedButton(button)
        }
    }
    
//**********************************************************************************
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        axis = .Horizontal
        distribution = .FillEqually
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 设置UI
    private func setupUI(){
        
        addChildButton("最近", imgName: "compose_emotion_table_left", type: .Recent)
        addChildButton("默认", imgName: "compose_emotion_table_mid", type: .Default)
        addChildButton("Emoj", imgName: "compose_emotion_table_mid", type: .Emoji)
        addChildButton("浪小花", imgName: "compose_emotion_table_right", type: .Lxh)
    }

    
    private func addChildButton(title: String,imgName: String, type: HMEmoticonType){
        let button = UIButton()
        
        button.setTitle(title, forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Selected)
        button.tag = type.rawValue
        button.setBackgroundImage(UIImage(named: "\(imgName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(imgName)_selected"), forState: UIControlState.Selected)
        // 为按钮注册点击事件
        button.addTarget(self, action: "emoticonButtonClick:", forControlEvents: .TouchUpInside)
        // 把按钮添加到父控件上
        addArrangedSubview(button)
        
        
    }
    
    @objc private func emoticonButtonClick(button: UIButton){
    
        if currentSelectedButton == button {
            return
        }
        
        
        selectedButton(button)
//        // 把之前的按钮取消选中
//        currentSelectedButton?.selected = false
//        // 把之前的按钮取消选中
//        currentSelectedButton?.selected = false
//        
//        button.selected = true
//        
//        currentSelectedButton = button
        // 这里不用判断代理是否实现了这个方法吗?
        delegate?.didselectedButtonWithType(HMEmoticonType(rawValue: button.tag)!)
        
    }
    
    private func selectedButton(button: UIButton) {
        // 把之前的按钮取消选中
        currentSelectedButton?.selected = false
        // 选中当前的
        button.selected = true
        // 记录当前的按钮
        currentSelectedButton = button
    }
    
}
//
